package com.example.task1.common;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.NewsListBean;
import com.example.task1.ui.BaseAdapter;
import com.example.task1.ui.base.NewsDetailsActivity;

import java.util.ArrayList;
import java.util.List;

public class CommonRvAdapter extends BaseAdapter<NewsListBean.RowsBean> {

    private List<NewsListBean.RowsBean> mData = new ArrayList<>();
    private ImageView ivImage;
    private TextView tvTitle;
    private TextView tvContext;
    private TextView tvTime;
    private TextView tvComment;
    private Context mContext;

    public CommonRvAdapter(Context context) {
        mContext = context;
    }

    @Override
    public void setData(List<NewsListBean.RowsBean> data){
        int size = mData.size();
        mData.addAll(data);
        notifyItemInserted(size);
    }


    @Override
    protected int getLayout() {
        return R.layout.commonrvlayout;
    }

    @Override
    protected Context getContext() {
        return mContext;
    }

    @Override
    protected List<NewsListBean.RowsBean> getData() {
        return mData;
    }

    @Override
    protected void onBind(myHolder holder, NewsListBean.RowsBean date) {
        MyApplication.glide(ivImage,date.getCover());
        tvTitle.setText(date.getTitle());
        tvContext.setText(Html.fromHtml(date.getContent()));
        tvTime.setText(date.getUpdateTime());
        tvComment.setText(date.getCommentNum()+"条评论");
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, NewsDetailsActivity.class);
                intent.putExtra("id",date.getId());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    protected void holder(View itemView) {
        initView(itemView);
    }

    private void initView(View itemView) {
        ivImage = itemView.findViewById(R.id.iv_image);
        tvTitle = itemView.findViewById(R.id.tv_title);
        tvContext = itemView.findViewById(R.id.tv_context);
        tvTime = itemView.findViewById(R.id.tv_time);
        tvComment = itemView.findViewById(R.id.tv_comment);
    }
}
